﻿using MediatR;
using SheepCultivation.Common.Domain.Beginning;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;
using System.Collections.Generic;

namespace SheepCultivation.API.AddliCation.Reportw
{
    public class ReportSeachHandler : IRequestHandler<ReportSeachCommon, Result>
    {
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public readonly IBaseRepository<BirthMessage> BirthMessageRepository;
        public readonly IBaseRepository<FemalePeriodic> FemalePeriodicRepository;
        public readonly IBaseRepository<MalePeriodic> MalePeriodicRepository;
        public readonly IBaseRepository<ProgenyPeriodic> ProgenyPeriodicRepository;
        public readonly IBaseRepository<BuildingsRecordEnter> BuildingsRecordEnterRepository;
        public readonly IBaseRepository<BuildingsRecordOut> BuildingsRecordOutRepository;

        public ReportSeachHandler(IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<BirthMessage> birthMessageRepository, IBaseRepository<FemalePeriodic> femalePeriodicRepository, IBaseRepository<MalePeriodic> malePeriodicRepository, IBaseRepository<ProgenyPeriodic> progenyPeriodicRepository, IBaseRepository<BuildingsRecordEnter> buildingsRecordEnterRepository, IBaseRepository<BuildingsRecordOut> buildingsRecordOutRepository)
        {
            BasicInformationRepository = basicInformationRepository;
            BirthMessageRepository = birthMessageRepository;
            FemalePeriodicRepository = femalePeriodicRepository;
            MalePeriodicRepository = malePeriodicRepository;
            ProgenyPeriodicRepository = progenyPeriodicRepository;
            BuildingsRecordEnterRepository = buildingsRecordEnterRepository;
            BuildingsRecordOutRepository = buildingsRecordOutRepository;
        }

        public async Task<Result> Handle(ReportSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var Count = new CountDto();
                if (request.StartTime != null && request.EndTime != null)
                {
                    var Data = FemalePeriodicRepository.GetAll().Where(x => x.FemalePeriodicPeriodicDate >= request.StartTime && x.FemalePeriodicPeriodicDate <= request.EndTime).ToList();
                    var Data1 = MalePeriodicRepository.GetAll().Where(x => x.MalePeriodicPeriodicDate >= request.StartTime && x.MalePeriodicPeriodicDate <= request.EndTime).ToList();
                    var Data2 = ProgenyPeriodicRepository.GetAll().Where(x => x.ProgenyPeriodicPeriodicDate >= request.StartTime && x.ProgenyPeriodicPeriodicDate <= request.EndTime).ToList();
                    var Data3 = BuildingsRecordEnterRepository.GetAll().Where(x => x.EnterDate >= request.StartTime && x.EnterDate <= request.EndTime).ToList();
                    var Data4 = BuildingsRecordOutRepository.GetAll().Where(x => x.OutDate >= request.StartTime && x.OutDate <= request.EndTime).ToList();
                    Count.BeginningSum = Data.Count + Data1.Count + Data2.Count;
                    Count.TransferEnterSum = Data3.Count;
                    Count.TransferOutSum = Data4.Count;

                }
                res.Code = (int)CommonEnumCode.success;
                res.Data = Count;
                res.Msg = "羊场实时存栏列表展示";
            }
            catch (Exception)
            {

                throw;
            }

            return res;
        }
    }
    public class CountDto
    {
        /// <summary>
        /// 期初总计
        /// </summary>
        public int BeginningSum { get; set; }
        /// <summary>
        /// 转舍总计(转入)
        /// </summary>
        public int TransferEnterSum { get; set; }
        /// <summary>
        /// 死亡离场数量
        /// </summary>
        public int DeathSum { get; set; }
        /// <summary>
        /// 淘汰离场数量
        /// </summary>
        public int EiminateSum { get; set; }
        /// <summary>
        /// 销售离场数量
        /// </summary>
        public int SaleSum { get; set; }
        /// <summary>
        /// 转舍总计(转出)
        /// </summary>
        public int TransferOutSum { get; set; }
        /// <summary>
        /// 期末
        /// </summary>
        public int FinalSum { get; set; }
    }
}
